package t2dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import t2bean.LendBookRecordBean;
import t2jdbc.JDBCUtil;

public class LendBookRecordDao extends Dao {
	private SetSql ss = new SetSql();

	public int insert(LendBookRecordBean lend) {
		String sql = "insert into lendrecord(bookid,cardid) values(?,?)";
		List<Object> list = new ArrayList<>();
		list.add(lend.getBookId());
		list.add(lend.getCardId());
		try {
			return dml(sql, list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public List<LendBookRecordBean> select(LendBookRecordBean lend) throws SQLException {
		List<LendBookRecordBean> backList = new ArrayList<>();
		ResultSet rs = null;
		List<Object> list = new ArrayList<>();
		String sql;
		if (lend != null) {
			sql = "select * from lendrecord where ";
			sql = ss.setSql(lend, sql, list, 2);
		} else {
			sql = "select * from lendrocord";
		}
		try {
//			System.out.println(sql);
			rs = dql(sql, list);
			while (rs.next()) {
				LendBookRecordBean lend1 = new LendBookRecordBean();
				lend1.setId(rs.getInt("id"));
				lend1.setBookId(rs.getInt("bookid"));
				lend1.setCardId(rs.getInt("cardid"));
				backList.add(lend1);
			}
//			System.out.println(backList.get(0));
			return backList;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(super.conn, super.ps, super.rs);
			super.conn = null;
			super.ps = null;
			super.rs = null;
			rs.close();
			rs = null;
		}
		return null;
	}
}
